/*
 * @Descripttion:
 * @LastEditors: xzh
 * @LastEditTime: 2023-08-05 21:57:24
 */
import { object_Inf } from "@/utils/typings/index.type";
import Dile_Methods from "@/utils/methods";
import {
  pagination_Inf,
  tableHeadAttr_Inf,
  tableHeadParam_Inf,
} from "./index.type";

/*
 * @Descripttion:表格工具类
 * @LastEditors: xzh
 * @LastEditTime: 2023-07-24 10:43:26
 */
export class Table_Util extends Dile_Methods {
  /**
   * @Author: xzh
   * @Descripttion:默认分页参数
   * @Param:
   */
  static defaultPagination: pagination_Inf = {
    page: 1,
    rows: 20,
    records: 0,
  };
  /**
   * @Author: xzh
   * @Descripttion:固定表头数据
   * @Param:
   */
  static fixedHeadArr: Array<tableHeadAttr_Inf> = [];
  /**
   * @Author: xzh
   * @Descripttion:格式化表头数据
   * @Param:
   */
  static formatTableHeadArr(
    tableDataArr: Array<object_Inf>,
    tableHeadParam: tableHeadParam_Inf | object_Inf = {}
  ) {
    let { attrArr: tableHeadArr = [] } = tableHeadParam;
    //排除表格没有的字段
    if ((tableDataArr || []).length > 0) {
      //扁平化所以子级
      let floatArr: Array<object_Inf> = [];
      this.flatAllChildren(floatArr, {
        children: tableDataArr,
      });
      tableHeadArr = tableHeadArr.filter(
        (x: object_Inf) =>
          !!floatArr.find((row) => {
            //兼容Name多层
            let valueMap = row;
            x.Name.split(".").forEach(
              (tag: string) => (valueMap = (valueMap || {})[tag])
            );
            return !!valueMap;
          })
      );
    }
    //按照Name去重
    return this.uniqueArr(tableHeadArr, "Name") || [];
  }
  /**
   * @Author: xzh
   * @Descripttion:获取固定表头
   * @Param:
   * @param {tableHeadParam_Inf} tableHeadParam
   */
  static getFixHeadArr(tableHeadParam: tableHeadParam_Inf | object_Inf) {
    let { filterFixHead } = tableHeadParam || {};
    return (
      this.fixedHeadArr
        //排除传入筛选字段
        .filter((x) =>
          filterFixHead == true
            ? false
            : !((filterFixHead || []) as Array<string>).includes(x.Name)
        )
    );
  }
  /**
   * @Author: xzh
   * @Descripttion:扁平化所有子级
   * @Param:
   * @param {Array} floatArr
   * @param {object_Inf} item
   */
  static flatAllChildren(floatArr: Array<object_Inf>, item: object_Inf) {
    Object.assign(
      floatArr,
      floatArr.concat(item.children || item.Children || []).filter((x) => !!x)
    );
    (item.children || item.Children || []).forEach((_item: object_Inf) => {
      this.flatAllChildren(floatArr, _item || {});
    });
  }
  /**
   * @Author: xzh
   * @Descripttion:格式化表格列值
   * @Param:
   * @param {object_Inf} scope
   * @param {tableHeadAttr_Inf} tableHead-表头信息
   */
  static formatColumnValue(
    scope: object_Inf,
    tableHead: tableHeadAttr_Inf | object_Inf
  ) {
    let { column, row } = scope,
      { property } = column;
    if (!property) {
      return false;
    }
    //兼容Name多层
    let valueMap = row;
    property
      .split(".")
      .forEach((tag: string) => (valueMap = (valueMap || {})[tag]));

    return valueMap;
  }
}
